/*
 *    Copyright (c) 2018-2025, cloud All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: cloud
 */
package com.fuyao.cloud.personnel.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fuyao.cloud.admin.api.entity.SysUserinfo;
import com.fuyao.cloud.admin.api.feign.RemoteUserInfoService;
import com.fuyao.cloud.admin.api.feign.RemoteUserService;
import com.fuyao.cloud.admin.api.vo.UserVO;
import com.fuyao.cloud.common.core.util.R;
import com.fuyao.cloud.common.excel.vo.ErrorMessage;
import com.fuyao.cloud.personnel.api.constant.ContractInfoConstant;
import com.fuyao.cloud.personnel.api.entity.ContractInfo;
import com.fuyao.cloud.personnel.api.vo.excel.ContractInfoExcelVO;
import com.fuyao.cloud.personnel.mapper.ContractInfoMapper;
import com.fuyao.cloud.personnel.service.ContractInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 合同信息
 *
 * @author 碎月
 * @date 2022-01-12 15:16:20
 */
@Service
public class ContractInfoServiceImpl extends ServiceImpl<ContractInfoMapper, ContractInfo> implements ContractInfoService {

    @Resource
    private RemoteUserInfoService userInfoService;

    @Resource
    private RemoteUserService userService;

    @Override
    public R importContractInfo(List<ContractInfoExcelVO> excelVOList, BindingResult bindingResult) {
        // 通用校验获取失败的数据
        List<ErrorMessage> errorMessageList = (List<ErrorMessage>) bindingResult.getTarget();

        for (int i = 0; i < excelVOList.size(); i++) {
            ContractInfoExcelVO infoExcelVO = excelVOList.get(i);
            Set<String> errorMsg = new HashSet<>();
            SysUserinfo userinfo =this.userInfoService.getByPersonnelNum(infoExcelVO.getPersonnelNum()).getData();
            if (userinfo == null || userinfo.getId() == null){
                errorMsg.add(String.format("人员编号不存在"));
            }
            UserVO userVO= userService.getUserName(userinfo.getUserId()).getData();
            // 数据合法情况
            if (CollUtil.isEmpty(errorMsg)) {
                insertExcelEducation(infoExcelVO,userinfo.getId(),userVO.getDeptName());
            }
            else {
                // 数据不合法情况
                errorMessageList.add(new ErrorMessage((long) (i + 2), errorMsg));
            }

        }

        if (CollUtil.isNotEmpty(errorMessageList)) {
            return R.failed(errorMessageList);
        }
        return R.ok();
    }

    private void insertExcelEducation(ContractInfoExcelVO infoExcelVO,Long userInfoId,String deptName) {
        ContractInfo contractInfo =new ContractInfo();
        BeanUtils.copyProperties(infoExcelVO,contractInfo);
        contractInfo.setUserInfoId(userInfoId);
        contractInfo.setDeptName(deptName);
        if(StrUtil.isNotEmpty(infoExcelVO.getContractStartDate())){
            contractInfo.setContractStartDate(DateUtil.parseDate(infoExcelVO.getContractStartDate()).toLocalDateTime().toLocalDate());
        }
        if(StrUtil.isNotEmpty(infoExcelVO.getContractEndDate())){
            contractInfo.setContractEndDate(DateUtil.parseDate(infoExcelVO.getContractEndDate()).toLocalDateTime().toLocalDate());
        }
        switch (infoExcelVO.getType()){
            case ContractInfoConstant.CONTRACT_TYPE_WORKER:
                contractInfo.setType(ContractInfoConstant.CONTRACT_TYPE_WORKER_VAL);
                break;
            case ContractInfoConstant.CONTRACT_TYPE_ZERO_HOURS:
                contractInfo.setType(ContractInfoConstant.CONTRACT_TYPE_ZERO_HOURS_VAL);
                break;
            case ContractInfoConstant.CONTRACT_TYPE_HIRE:
                contractInfo.setType(ContractInfoConstant.CONTRACT_TYPE_HIRE_VAL);
                break;
        }
        this.baseMapper.insert(contractInfo);
    }
}
